package com.ibeeking.found.log.rest.service.impl;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.log.rest.service.ILogCodeGroupService;
import com.ibeeking.found.log.service.common.bo.LogCodeGroupBO;
import com.ibeeking.found.log.service.common.param.LogCodeGroupAddByCodeParam;
import com.ibeeking.found.log.service.common.vo.LogCodeSimpleVO;
import com.ibeeking.found.log.service.mapper.LogCodeGroupMapper;
import com.ibeeking.found.log.service.common.param.LogCodeGroupParam;
import com.ibeeking.found.log.service.common.vo.LogCodeGroupVO;
import com.ibeeking.found.log.service.common.dos.LogCodeGroupDO;
import com.ibeeking.found.log.service.common.query.LogCodeGroupQuery;
import com.ibeeking.found.log.service.common.query.LogCodeGroupPageQuery;
import com.ibeeking.nematos.utils.data.Collections3;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName LogCodeGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-25 17:54
 **/
@Service
public class LogCodeGroupServiceImpl extends ServiceImpl<LogCodeGroupMapper, LogCodeGroupDO> implements ILogCodeGroupService {

    @Resource
    private LogCodeGroupMapper logCodeGroupMapper;

    @Override
    public List<LogCodeGroupVO> list(LogCodeGroupQuery logCodeGroupQuery) {
        List<LogCodeGroupDO> logCodeGroupDOS = logCodeGroupMapper.selectList(new LambdaQueryWrapper<LogCodeGroupDO>()
                .eq(logCodeGroupQuery.getCodeId() != null, LogCodeGroupDO::getCodeId, logCodeGroupQuery.getCodeId())
                .eq(logCodeGroupQuery.getGroupId() != null, LogCodeGroupDO::getGroupId, logCodeGroupQuery.getGroupId())
                .orderByDesc(LogCodeGroupDO::getCreateTime));
        return JsonUtils.jsonToList(logCodeGroupDOS, LogCodeGroupVO.class);
    }

    @Override
    public Page<LogCodeGroupVO> pageList(LogCodeGroupPageQuery logCodeGroupPageQuery) {
        Page<LogCodeGroupBO> logCodeGroupBOPage = logCodeGroupMapper.selectPageList(new Page<LogCodeGroupDO>(logCodeGroupPageQuery.getPageNum(), logCodeGroupPageQuery.getPageSize()), logCodeGroupPageQuery);
        return BeanUtil.convertPage(logCodeGroupBOPage, LogCodeGroupVO.class);
    }

    @Override
    public List<LogCodeSimpleVO> selByGroupId(Long groupId) {
        LogCodeGroupQuery logCodeGroupQuery = new LogCodeGroupQuery();
        logCodeGroupQuery.setGroupId(groupId);
        List<LogCodeGroupBO> records = logCodeGroupMapper.selectBoList(logCodeGroupQuery);
        return records.parallelStream().map(x -> {
            LogCodeSimpleVO logCodeSimpleVO = new LogCodeSimpleVO();
            logCodeSimpleVO.setId(x.getCodeId());
            logCodeSimpleVO.setCode(x.getCode());
            logCodeSimpleVO.setDescription(x.getCodeDescription());
            return logCodeSimpleVO;
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean add(LogCodeGroupParam logCodeGroupParam) {
        LogCodeGroupDO logCodeGroupDO = BeanUtil.convertBean(logCodeGroupParam, LogCodeGroupDO.class);
        logCodeGroupDO.setCreateTime(LocalDateTime.now());

        return logCodeGroupMapper.insert(logCodeGroupDO) > 0;
    }

    @Override
    public Boolean addByCodeId(LogCodeGroupAddByCodeParam param) {
        Long userId = UserUtils.getUserId();
        Long codeId = param.getCodeId();
        List<Long> groupIdList = param.getGroupIdList();
        this.delByCodeId(codeId);
        if (Collections3.isNotEmpty(groupIdList)) {
            List<LogCodeGroupDO> dooList = groupIdList.stream().map(x -> {
                LogCodeGroupDO doo = new LogCodeGroupDO();
                doo.setCreateBy(userId);
                doo.setCodeId(codeId);
                doo.setGroupId(x);
                doo.setCreateTime(LocalDateTime.now());

                return doo;
            }).collect(Collectors.toList());
            logCodeGroupMapper.insertBatchSomeColumn(dooList);
        }
        return true;
    }

    @Override
    public Boolean modify(LogCodeGroupParam logCodeGroupParam) {
        LogCodeGroupDO logCodeGroupDO = BeanUtil.convertBean(logCodeGroupParam, LogCodeGroupDO.class);

        return logCodeGroupMapper.updateById(logCodeGroupDO) > 0;
    }

    @Override
    public LogCodeGroupVO view(Long id) {
        LogCodeGroupDO logCodeGroupDO = logCodeGroupMapper.selectById(id);
        return BeanUtil.convertBean(logCodeGroupDO, LogCodeGroupVO.class);
    }

    @Override
    public Boolean del(Long id) {
        return logCodeGroupMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean delByCodeId(Long codeId) {
        if (codeId == null) {
            return false;
        }
        return logCodeGroupMapper.physicalDelByCodeId(codeId) > 0;
    }

    @Override
    public Boolean delByGroupId(Long groupId) {
        if (groupId == null) {
            return false;
        }
        return logCodeGroupMapper.deleteById(groupId) > 0;
    }
}